<?php

/**
 * uniBoard Settings list
 *
 * Light and fast forum solution for web 2.0-like communities.
 *
 * @package		uniBoard
 * @author		Rafał Pitoń
 * @license		http://www.gnu.org/licenses/gpl-3.0.html
 * @since		Version 0.1
 * 
 * ------------------------------------------------------------------------
 * 
 * $Date: 2010-09-08 03:38:35 +0000 (Wed, 08 Sep 2010) $
 * $Revision: 60 $
 * $Author: rafio.xudb@gmail.com $
 */

// ------------------------------------------------------------------------

class action_settings{
	
	/**
	 * Main class pointer
	 *
	 * @var object
	 */
	
	private $appCore		= NULL;
	
	/**
	 * Settings group API
	 *
	 * @var object
	 */
	
	private $api			= NULL;
	
	/**
	 * Group items
	 *
	 * @var array
	 */
	
	private $settings		= array();
	
	// ------------------------------------------------------------------------

	/**
	 * Initialise Action
	 *
	 * @param object $appCore
	 */
	
	function __construct( $appCore){
		
		// Store appCore pointer
		$this -> appCore = $appCore;
	
		// Start settings group API
		$this -> api = new api_settings( $appCore, 0, $appCore -> _REQ( 'settings'));
		
		// Did we loaded api from DB?
		if ( $this -> api -> error)
		{
			// Draw last error
			$appCore -> output -> error( $appCore -> lang['acp_settings_group_not_found'], $appCore -> lang['acp_settings_groups']);
			
			// Trow user back to groups list
			new action_settings_list( $appCore);	
		}
		else
		{
			// Select group's items
			$appCore -> db -> query( 'SELECT *
			FROM ' .  DB_PREFIX . 'settings
			WHERE setting_group = \'' . $appCore -> db -> strEscape( $this -> api -> group_key) . '\'
			ORDER BY setting_pos');
			
			// Found settings?
			if ( $appCore -> db -> numRows() == 0)
			{
				// Free result
				$appCore -> db -> freeResult();
				
				// Draw Error
				$appCore -> output -> error( $appCore -> lang['acp_settings_group_empty'], $appCore -> ifString( $this -> api -> group_name));
			
				// Repeat actions list
				new action_settings_list( $appCore);	
			}
			else
			{
				// Load fields templates
				$appCore -> templates['form_inputs'] = new tpl_form_inputs( $appCore);
				
				// Set page help using group help
				$appCore -> output -> page_help = $appCore -> ifString( $this -> api -> group_description);
				
				// Updating settings?
				$update = (bool) $appCore -> _POST( 'update');
				
				// Block opened?
				$block_open = false;
				
				// Start output buffer
				ob_start();
				
				// Open form
				echo $appCore -> output -> openForm( $appCore -> adminLink( 'act=settings&settings=' . $this -> api -> group_key));
				
				// Hidden inout
				echo $appCore -> drawHidden( 'update', 1);
				
				// Parse result
				while ( $setting_result = $appCore -> db -> fetch_assoc())
				{
					// Block opened?
					if ( !$block_open || !empty( $setting_result['setting_section']))
					{
						// Close previous block?
						if ( $block_open)
						{
							echo $appCore -> output -> closeFormBlock();
						}
						
						// Draw opening template
						echo $appCore -> output -> openFormBlock( $appCore -> ifString( $setting_result['setting_section']));
						
						// Block opened
						$block_open = true;				
					}
										
					// Setting field not info?
					if ( $setting_result['setting_field'] != 'info')
					{
						// Parse extra?
						if ( isset( $setting_result['setting_extra'][0]))
						{
							$setting_result['setting_extra'] = $appCore -> parseExtra( $setting_result['setting_extra']);
						}
						else
						{
							// Empty extra
							$setting_result['setting_extra'] = array();
						}
						
						// Update setting
						if ( $update)
						{							
							// New value
							$new_val = $appCore -> _POST( 'setting_' . $setting_result['setting_id']);
							
							// Field is list?
							if ( $setting_result['setting_field'] == 'list')
							{
								// Value exists in array?
								if ( is_array( $new_val) || !isset( $new_val[0]) || !isset( $setting_result['setting_extra'][$new_val]))
								{
									// Make new val empty val
									$new_val = $setting_result['setting_value_default'];
								}
							}
							else if ( $setting_result['setting_field'] == 'multi_list')
							{
								
							}
							else
							{
								// New value is empty?
								if ( is_array( $new_val) || !isset( $new_val[0]))
								{
									// Make new val empty val
									$new_val = $setting_result['setting_value_default'];
								}
								else if ( isset( $setting_result['setting_extra']))
								{
									// Set type?
									if ( isset( $setting_result['setting_value_type'][0]))
									{
										settype( $new_val, $setting_result['setting_value_type']);
									}
									
									// Min val?
									if ( isset( $setting_result['setting_extra']['MIN_VAL']) && $new_val < $setting_result['setting_extra']['MIN_VAL'])
									{
										$new_val = $setting_result['setting_extra']['MIN_VAL'];
									}
									
									// Max val?
									if ( isset( $setting_result['setting_extra']['MAX_VAL']) && $new_val > $setting_result['setting_extra']['MAX_VAL'])
									{
										$new_val = $setting_result['setting_extra']['MAX_VAL'];
									}
								}
								else
								{
									// Set type?
									if ( isset( $setting_result['setting_value_type'][0]))
									{
										settype( $new_val, $setting_result['setting_value_type']);
									}
								}
							}
							
							// Update setting position?
							if ( DEVELOPER_MODE)
							{
								// Get Setting Position
								$new_pos = (int) $appCore -> _POST( 'pos_setting_' . $setting_result['setting_id']);
								
								// Update field
								$appCore -> db -> update( array( 'setting_value' => $appCore -> db -> strEscape( $new_val), 'setting_pos' => $new_pos), 'settings', 'setting_id = \'' . $appCore -> db -> strEscape( $setting_result['setting_id']) . '\'');
							
								// Overwrite result
								$setting_result['setting_value'] = $new_val;
								$setting_result['setting_pos'] = $new_pos;
							}
							else
							{
								// Update field
								$appCore -> db -> update( array( 'setting_value' => $appCore -> db -> strEscape( $new_val)), 'settings', 'setting_id = \'' . $appCore -> db -> strEscape( $setting_result['setting_id']) . '\'');
							
								// Overwrite result
								$setting_result['setting_value'] = $new_val;
							}
						}
						
						// Display field
						switch ( $setting_result['setting_field'])
						{
							// Text input
							case 'text':
								$setting_field = $appCore -> drawTextInput( 'setting_' . $setting_result['setting_id'], $setting_result['setting_value']);
							break;
							
							// Text area
							case 'text_area':
								$setting_field = $appCore -> drawTextArea( 'setting_' . $setting_result['setting_id'], $setting_result['setting_value']);
							break;
							
							// List
							case 'list':
								$setting_field = $appCore -> drawList( 'setting_' . $setting_result['setting_id'], $setting_result['setting_extra'], $setting_result['setting_value']);
							break;
							
							// Yes-No switch
							case 'yes_no':
								$setting_field = $appCore -> drawYesNo( 'setting_' . $setting_result['setting_id'], $setting_result['setting_value']);
							break;
						}
					}
					
					// Draw field
					echo $appCore -> output -> formItem( $appCore -> ifString( $setting_result['setting_name']), ( $setting_result['setting_field'] == 'info' ? htmlspecialchars($setting_result['setting_value']) : $setting_field), $appCore -> ifString( $setting_result['setting_description']));
					
					// Draw position?
					if ( DEVELOPER_MODE)
					{
						// Draw Additional field with position
						echo $appCore -> output -> formItem( $appCore -> lang['acp_setting_position'], $appCore -> drawTextInput( 'pos_setting_' . $setting_result['setting_id'], $setting_result['setting_pos'], '60px'));
					}
				}
				
				// Free result
				$appCore -> db -> freeResult();
				
				// Draw button
				echo $appCore -> output -> submitButton( $appCore -> lang['change_settings']);
				
				// Close form block
				echo $appCore -> output -> closeFormBlock();
				
				// Close form
				echo $appCore -> output -> closeForm();
				
				// Show "updated" message?
				if ( $update)
				{
					// Show message
					$appCore -> output -> message( $appCore -> lang['acp_settings_changed'], $appCore -> lang['acp_settings_groups']);
					
					// Kill cache
					class_cache::flushCache( 'configuration');
				}
				
				// Draw output
				$appCore -> output -> draw( $appCore -> ifString( htmlspecialchars( $this -> api -> group_name)), array( $appCore -> adminLink( 'act=settings_list') => $appCore -> lang['acp_settings_groups']), ob_get_clean());
			}
		}
		
	}

}